package com.hzdracom.epubreader.views;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.shwread.android.qysw10000243.R;

import net.nightwhistler.pageturner.Configuration;

import roboguice.RoboGuice;


public class FontSettingsView extends LinearLayout implements OnClickListener {

	private static final int MIN_FONT_SIZE = 12;
	private static final int MAX_FONT_SIZE = 25;
	private static final int LINE_SPACE_1 = 0;
	private static final int LINE_SPACE_2 = 8;
	private static final int LINE_SPACE_3 = 16;

	FontSettingsListener listener;
	Button fontSizePlus;
	Button fontSizeSub;
	ImageButton btnLineSpace3;
	ImageButton btnLineSpace2;
	ImageButton btnLineSpace1;
	Button btnFontFamily1;
	Button btnFontFamily2;
	Button btnFontFamily3;
	Configuration config;

	public void setListener(FontSettingsListener linsener) {
		this.listener = linsener;
	}

	public FontSettingsView(Context context) {
		super(context);
		Init(context);
	}

	public FontSettingsView(Context context, AttributeSet attrs) {
		super(context, attrs);
		Init(context);
	}

	private void Init(Context context) {
		config = RoboGuice.getInjector(getContext()).getInstance(
				Configuration.class);

		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.epub3_view_font_settings, this);

		fontSizePlus = (Button) this.findViewById(R.id.btnFontSizePlus);
		fontSizeSub = (Button) this.findViewById(R.id.btnFontSizeSub);

		fontSizePlus.setOnClickListener(this);
		fontSizeSub.setOnClickListener(this);

		btnLineSpace1 = (ImageButton) this.findViewById(R.id.btnLineSpace1);
		btnLineSpace2 = (ImageButton) this.findViewById(R.id.btnLineSpace2);
		btnLineSpace3 = (ImageButton) this.findViewById(R.id.btnLineSpace3);

		btnLineSpace1.setOnClickListener(this);
		btnLineSpace2.setOnClickListener(this);
		btnLineSpace3.setOnClickListener(this);

		btnFontFamily1 = (Button) this.findViewById(R.id.btnFontFamily1);
		btnFontFamily1.setOnClickListener(this);

		btnFontFamily2 = (Button) this.findViewById(R.id.btnFontFamily2);
		btnFontFamily2.setOnClickListener(this);

		btnFontFamily3 = (Button) this.findViewById(R.id.btnFontFamily3);
		btnFontFamily3.setOnClickListener(this);
		
		updateCurrentUIStatus();
	}

	public interface FontSettingsListener {

		void updateFontFamily(String fontName);

		void updateFontSize(int fontSize);

		void updateParaHeight(int heightLevel);

		void updateColorTheme(int theme);
	}

	@Override
	public void onClick(View v) {
		if (listener != null) {

			switch (v.getId()) {
			case R.id.btnFontSizePlus:
				fontSizePlus();
				break;

			case R.id.btnFontSizeSub:
				fontSizeSub();
				break;
			case R.id.btnLineSpace1:
				changeLineSpace(LINE_SPACE_1);
				break;
			case R.id.btnLineSpace2:
				changeLineSpace(LINE_SPACE_2);
				break;
			case R.id.btnLineSpace3:
				changeLineSpace(LINE_SPACE_3);
				break;
			case R.id.btnFontFamily1:
				changeFontFamily(Configuration.ChineseFontFamily.DEFAULT);
				break;
			case R.id.btnFontFamily2:
				changeFontFamily(Configuration.ChineseFontFamily.KAITI);
				break;
			case R.id.btnFontFamily3:
				changeFontFamily(Configuration.ChineseFontFamily.SONGTI);
				break;
			default:
				break;
			}

		}

	}

	public void updateCurrentUIStatus() {
		// 行距
		if (config.getLineSpacing() == LINE_SPACE_1) {
			// 紧凑
			this.btnLineSpace1.setImageResource(R.drawable.epub3_linespace_1_pressed);
			this.btnLineSpace2.setImageResource(R.drawable.epub3_linespace_2);
			this.btnLineSpace3.setImageResource(R.drawable.epub3_linespace_3);
		} else if (config.getLineSpacing() == LINE_SPACE_2) {
			// 正常
			this.btnLineSpace1.setImageResource(R.drawable.epub3_linespace_1);
			this.btnLineSpace2.setImageResource(R.drawable.epub3_linespace_2_pressed);
			this.btnLineSpace3.setImageResource(R.drawable.epub3_linespace_3);
		} else if (config.getLineSpacing() == LINE_SPACE_3) {
			// 宽松
			this.btnLineSpace1.setImageResource(R.drawable.epub3_linespace_1);
			this.btnLineSpace2.setImageResource(R.drawable.epub3_linespace_2);
			this.btnLineSpace3.setImageResource(R.drawable.epub3_linespace_3_pressed);
		}

		int selectColor = Color.rgb(0, 104, 183);
		int normalColor = Color.rgb(128, 128, 128);

		// 字体
		if (config.getDefaultFontFamilyName().equals(
				Configuration.CN_FONT_SYS_DEFAULT)) {
			this.btnFontFamily1.setTextColor(selectColor);
			this.btnFontFamily2.setTextColor(normalColor);
			this.btnFontFamily3.setTextColor(normalColor);
		} else if (config.getDefaultFontFamilyName().equals(
				Configuration.CN_FONT_KAITI)) {
			this.btnFontFamily1.setTextColor(normalColor);
			this.btnFontFamily2.setTextColor(selectColor);
			this.btnFontFamily3.setTextColor(normalColor);
		} else if (config.getDefaultFontFamilyName().equals(
				Configuration.CN_FONT_SONGTI)) {
			this.btnFontFamily1.setTextColor(normalColor);
			this.btnFontFamily2.setTextColor(normalColor);
			this.btnFontFamily3.setTextColor(selectColor);
		} else {
			this.btnFontFamily1.setTextColor(selectColor);
			this.btnFontFamily2.setTextColor(normalColor);
			this.btnFontFamily3.setTextColor(normalColor);
		}
		
		if(config.getTextSize() >= MAX_FONT_SIZE)
		{
			this.fontSizePlus.setEnabled(false);
			this.fontSizeSub.setEnabled(true);
			this.fontSizePlus.setTextColor(normalColor);
			this.fontSizeSub.setTextColor(selectColor);
		}
		else if(config.getTextSize() <= MIN_FONT_SIZE)
		{
			this.fontSizePlus.setEnabled(true);
			this.fontSizeSub.setEnabled(false);
			this.fontSizePlus.setTextColor(selectColor);
			this.fontSizeSub.setTextColor(normalColor);
		}
		else
		{
			this.fontSizePlus.setEnabled(true);
			this.fontSizeSub.setEnabled(true);
			this.fontSizePlus.setTextColor(selectColor);
			this.fontSizeSub.setTextColor(selectColor);
		}
	}

	/**
	 * 调整字体样式
	 * 
	 * @param font
	 */
	private void changeFontFamily(Configuration.ChineseFontFamily font) {

		String name;
		switch (font) {
		case DEFAULT:
			name = Configuration.CN_FONT_SYS_DEFAULT;
			break;
		case KAITI:
			name = Configuration.CN_FONT_KAITI;
			break;
		case SONGTI:
			name = Configuration.CN_FONT_SONGTI;
			break;
		default:
			name = Configuration.CN_FONT_SYS_DEFAULT;
			break;
		}

		config.setDefaultFontFamily(name);
		listener.updateFontFamily(name);
		updateCurrentUIStatus();
	}

	/**
	 * 调整行距
	 * 
	 */
	private void changeLineSpace(int space) {
		if (config.getLineSpacing() != space) {
			config.setLineSpacing(space);
			listener.updateParaHeight(space);
			updateCurrentUIStatus();
		}
	}

	private void fontSizePlus() {
		// 增加字体大小
		if (config.getTextSize() < MAX_FONT_SIZE) {
			int newSize = config.getTextSize() + 3;
			if (newSize > MAX_FONT_SIZE) {
				newSize = MAX_FONT_SIZE;
			}
			config.setTextSize(newSize);
			listener.updateFontSize(newSize);
			updateCurrentUIStatus();
		}
	}

	private void fontSizeSub() {
		// 减少字体大小
		if (config.getTextSize() > MIN_FONT_SIZE) {
			int newSize = config.getTextSize() - 3;
			if (newSize < MIN_FONT_SIZE) {
				newSize = MIN_FONT_SIZE;
			}
			config.setTextSize(newSize);
			listener.updateFontSize(newSize);
			updateCurrentUIStatus();
		}
	}

}
